package com.example.service;

import com.example.mapper.SearchMapper;
import com.example.pojo.bo.SearchBO;
import com.example.pojo.entity.Search;
import com.example.util.SearchKeywordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 本文来演示
 *
 * @author 赵建云
 * @create 2023/10/14
 */
@Service
public class SearchKeywordServiceImpl implements SearchKeywordService{
    @Autowired
    private SearchMapper searchMapper;
    @Override
    public int saveSearch(Integer jsId, String searchKeyword) {
        Integer count = 0;
        String searchKy = SearchKeywordUtil.handleSearchKeyword(searchKeyword);
        List<SearchBO> searchList = searchMapper.searchKeyword(jsId);
        if (null!=searchList&&searchList.size()>0) {
            for (SearchBO searchBO : searchList) {
                if (SearchKeywordUtil.includeSearch(searchKy, searchBO.getSearchKeyword())) {
                    count = searchMapper.updateSearch(searchBO.getId());
                } else if (SearchKeywordUtil.calculatePercentageSimilar(searchKy, searchBO.getSearchKeyword()) >= 65) {
                    count = searchMapper.updateSearch(searchBO.getId());
                }
            }
        }
        if (count==0){
            Search search = new Search();
            search.setJsId(jsId);
            search.setSearchKeyword(searchKeyword);
            search.setSearchTime(new Date());
            count = searchMapper.saveSearch(search);
        }

        return count;
    }
}
